﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Data.Linq;
using QLTVNEW.Properties;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace QLTVNEW
{
    public partial class frmAccount : DevComponents.DotNetBar.Office2007Form
    {
        public frmAccount()
        {
            InitializeComponent();
        }
        QLTVDataContext db = new QLTVDataContext();
        #region 'Thành Phần'
        void txtReadOnly(bool a)
        {
            txtTen.ReadOnly = a;
            txtPass.ReadOnly = a;

        }

        void btnThem(bool a)
        {
            btnupdate.Enabled = a;
            btndel.Enabled = a;
        }

        void btnSua(bool a)
        {
            btninsert.Enabled = a;
            btndel.Enabled = a;
        }

        void ThemCancel()
        {
            btninsert.Image = Resources.Cancel;
            superTooltip1.SetSuperTooltip(btninsert, new SuperTooltipInfo("Huỷ Bỏ Thao Tác Thêm.", "", "", null, null, eTooltipColor.Lemon, true, true, new System.Drawing.Size(135, 21)));
        }
        void ThemOK()
        {
            btninsert.Image = Resources.add1_;
            superTooltip1.SetSuperTooltip(btninsert, new SuperTooltipInfo("Thêm Tài Khoản.", "", "", null, null, eTooltipColor.Lemon, true, true, new System.Drawing.Size(130, 21)));
        }

        void SuaCancel()
        {
            btnupdate.Image = Resources.Cancel;
            superTooltip1.SetSuperTooltip(btnupdate, new SuperTooltipInfo("Huỷ Bỏ Thao Tác Sửa.", "", "", null, null, eTooltipColor.Lemon, true, true, new System.Drawing.Size(130, 21)));
        }

        void SuaOK()
        {
            btnupdate.Image = Resources.edit;
            superTooltip1.SetSuperTooltip(btnupdate, new SuperTooltipInfo("Sửa Tài Khoản.", "", "", null, null, eTooltipColor.Lemon, true, true, new System.Drawing.Size(130, 21)));
        }

        void LoadGridView()
        {
            var query = from tk in db.Accounts select tk;
            GVW.DataSource = query;
        }
        #endregion
        private void frmAccount_Load(object sender, EventArgs e)
        {
            LoadGridView();
            txtTen.Text = GVW.Rows[0].Cells[0].Value.ToString().Trim();
            txtPass.Text = GVW.Rows[0].Cells[1].Value.ToString().Trim();
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtReadOnly(false);
            foreach (Control ctr in groupBox1.Controls)
            {
                if (ctr is TextBox)
                    ctr.Text = "";
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.AutoValidate = AutoValidate.Disable;
            this.Close();
        }

        private void btninsert_Click(object sender, EventArgs e)
        {
            if (btnsave.Enabled == false)
            {
                btnclear.PerformClick();
                ThemCancel();
                txtReadOnly(false);
                btnThem(false);
                txtTen.Focus();
                btnsave.Enabled = true;
            }
            else
            {
                ThemOK();
                txtReadOnly(true);
                btnThem(true);
                btnsave.Enabled = false;
            }
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            if (btnsave.Enabled==false)
            {
                SuaCancel();
                btnSua(false);
                txtReadOnly(false);
                txtTen.ReadOnly = true;
                btnsave.Enabled = true;
                txtPass.Focus();
            } 
            else
            {
                SuaOK();
                btnSua(true);
                txtReadOnly(true);
                btnsave.Enabled = false;
            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            if (btnupdate.Enabled==false)
            {
                string err = "";
                db.uspCRUDAccount('t',txtTen.Text,txtPass.Text,ref err);
                MessageBox.Show(err.ToString(),"Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                db.SubmitChanges();
                txtReadOnly(true);
                btnThem(true);
                ThemOK();
                btnsave.Enabled=false;
                LoadGridView();
            }
            if (btninsert.Enabled==false)
            {
               if (txtTen.Text!="")
               {
                   Account tbac=db.Accounts.Where(a=>a.UserName==txtTen.Text).SingleOrDefault<Account>();
                   if (tbac!=null)
                   {
                       db.Connection.Open();
                       db.Transaction=db.Connection.BeginTransaction();
                       try
                       {
                           tbac.Pass=txtPass.Text;
                           db.SubmitChanges();
                           db.Transaction.Commit();
                           MessageBox.Show("Sửa Thành Công", "Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                           LoadGridView();
                       }
                       catch(Exception ex)
                       {
                            MessageBox.Show(ex.Message,"Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                           db.Transaction.Rollback();
                       }
                       finally
                       {
                           if (db.Connection.State==ConnectionState.Open)
                           {
                               db.Connection.Close();
                           }
                           db.Transaction=null;
                           txtReadOnly(true);
                           btnSua(true);
                           SuaOK();
                           btnsave.Enabled=false;

                       }
                   }

               }
                
            }

        }

        private void btndel_Click(object sender, EventArgs e)
        {
            if (txtTen.Text!="")
            {
                if (MessageBox.Show("Bạn Có Muốn Xóa Không ?","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
                {
                    string err = "";
                    db.uspCRUDAccount('x', txtTen.Text, txtPass.Text, ref err);
                    MessageBox.Show(err.ToString(),"Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    db.SubmitChanges();
                }
            }
            else
                MessageBox.Show("Bạn Chưa Nhập Tên Tài Khoản","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        int dong;
        private void GVW_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dong = e.RowIndex;
                txtTen.Text = GVW.Rows[dong].Cells[0].Value.ToString().Trim();
                txtPass.Text = GVW.Rows[dong].Cells[1].Value.ToString().Trim();
            }
            catch
            {
                return;
            }
        }
    }
}
